package cn.cq.admin.service.impl;

import java.util.Date;

import cn.cq.admin.entity.dto.BatchDto;
import cn.cq.admin.entity.model.BatchCourse;
import cn.cq.admin.entity.vo.BatchVo;
import cn.cq.admin.service.BatchCourseService;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cq.admin.entity.model.Batch;
import cn.cq.admin.service.BatchService;
import cn.cq.admin.mapper.BatchMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author  dicraft
 * @description 针对表【batch】的数据库操作Service实现
 * @createDate 2022-10-03 14:30:44
 */
@Service
public class BatchServiceImpl extends ServiceImpl<BatchMapper, Batch>
        implements BatchService {
    @Resource
    private BatchCourseService batchCourseService;

    @Override
    public SaResult doList(BatchDto batchDto) {
        Page<Batch> batchPage = new Page<>(batchDto.getPageNo(), batchDto.getPageSize());
        CollUtil.isNotEmpty(batchDto.getStartToEnd());
        LambdaQueryChainWrapper<Batch> wrapper = lambdaQuery()
                .like(StrUtil.isNotBlank(batchDto.getBatchName()), Batch::getBatchName, batchDto.getBatchName());
        if (CollUtil.isNotEmpty(batchDto.getStartToEnd())) {
            wrapper.ge(Batch::getStartTime, batchDto.getStartToEnd().get(0)).le(Batch::getEndTime, batchDto.getStartToEnd().get(1));
        }
        List<BatchVo> collect = wrapper
                .page(batchPage)
                .getRecords()
                .stream().map((batch -> {
                    List<List<String>> courseId = batchCourseService.lambdaQuery().eq(BatchCourse::getBatchId, batch.getId()).list()
                            .stream().map(batchCourse -> {
                                List<String> list = new ArrayList<>();
                                list.add(String.valueOf(batchCourse.getCourseId()));
                                return list;
                            }).collect(Collectors.toList());
                    BatchVo batchVo = new BatchVo();
                    batchVo.setBatchName(batch.getBatchName());
                    batchVo.setStatus(batch.getStatus());
                    batchVo.setId(String.valueOf(batch.getId()));
                    batchVo.setStartTime(batch.getStartTime());
                    batchVo.setEndTime(batch.getEndTime());
                    batchVo.setCourseId(courseId);
                    batchVo.setStartToEnd(new ArrayList<Date>(Arrays.asList(batch.getStartTime(), batch.getEndTime())));
                    return batchVo;
                })).collect(Collectors.toList());
        Page<BatchVo> batchVoPage = new Page<>();
        batchVoPage.setRecords(collect);
        batchVoPage.setTotal(batchPage.getTotal());
        batchVoPage.setSize(batchPage.getSize());
        batchVoPage.setCurrent(batchPage.getCurrent());
        return SaResult.data(batchVoPage);
    }

    @Override
    @Transactional
    public void doSave(BatchDto batchDto) {
        Batch batch = new Batch();
        batch.setBatchName(batchDto.getBatchName());
        List<Date> startToEnd = batchDto.getStartToEnd();
        batch.setStartTime(startToEnd.get(0));
        batch.setEndTime(startToEnd.get(1));
        batch.setStatus(batchDto.getStatus());
        save(batch);
        Long batchId = batch.getId();
        Long[] courseId = batchDto.getCourseId();
        List<BatchCourse> collect = Arrays.stream(courseId).map(id -> {
            BatchCourse batchCourse = new BatchCourse();
            batchCourse.setBatchId(batchId);
            batchCourse.setCourseId(id);
            return batchCourse;
        }).collect(Collectors.toList());
        batchCourseService.saveBatch(collect);
    }

    @Override
    @Transactional
    public void doRemove(List<Long> ids) {
        //删除批次
        removeBatchByIds(ids);
        //删除批次-课程表
        batchCourseService.lambdaUpdate().in(BatchCourse::getBatchId, ids).remove();
    }

    @Override
    @Transactional
    public void doModify(BatchDto batchDto) {
        Batch batch = new Batch();
        Long batchId = batchDto.getId();
        batch.setId(batchId);
        batch.setBatchName(batchDto.getBatchName());
        batch.setStartTime(batchDto.getStartToEnd().get(0));
        batch.setEndTime(batchDto.getStartToEnd().get(1));
        batch.setStatus(batchDto.getStatus());
        updateById(batch);
        Long[] courseId = batchDto.getCourseId();
        //先删除 在添加
        batchCourseService.lambdaUpdate().in(BatchCourse::getBatchId, batchId).remove();
        List<BatchCourse> collect = Arrays.stream(courseId).map(id -> {
            BatchCourse batchCourse = new BatchCourse();
            batchCourse.setBatchId(batchId);
            batchCourse.setCourseId(id);
            return batchCourse;
        }).collect(Collectors.toList());
        batchCourseService.saveBatch(collect);
    }

    @Override
    public void doModifyStatus(Long id,Integer status) {
        lambdaUpdate().eq(Batch::getId, id).set(Batch::getStatus, status).update();
    }

    @Override
    public List<BatchVo> doListAll() {
        return lambdaQuery().list().stream().map(batch -> {
            BatchVo batchVo = new BatchVo();
            batchVo.setBatchName(batch.getBatchName());
            batchVo.setStatus(batch.getStatus());
            batchVo.setStartTime(batch.getStartTime());
            batchVo.setEndTime(batch.getEndTime());
            batchVo.setId(String.valueOf(batch.getId()));
            return batchVo;
        }).collect(Collectors.toList());
    }

}




